/**
 * Creat with IntelliJ IDEA
 * Description:挂起、恢复和终止线程
 * 在下面的例子11中，线程thread每隔一秒钟输出一个整数，输出3个整数后，该线程挂起；主线程负责恢复thread线程继续执行。
 * User:WZW
 * Date:2021-11-28
 * Time:16:56
 */
class A implements Runnable{
    int number = 0;
    boolean stop = false;
    boolean getStop(){
        return stop;
    }
    public void run(){
        while(true){
            number++;
            System.out.println(Thread.currentThread().getName() + " : " + number);
            if(number==3){
                try{
                    System.out.println(Thread.currentThread().getName() + " : hang up");
                    stop = true;
                    hangUP();
                    System.out.println(Thread.currentThread().getName() + " : resumed");
                }catch(Exception e){}
            }
            try{
                Thread.sleep(1000);
            } catch(Exception e){}
        }
    }
    public synchronized void hangUP() throws InterruptedException{
        wait();
    }
    public synchronized void restart(){
        notifyAll();
    }
}

public class Example8_11 {
    public static void main(String args[])
    {
        A target = new A();
        Thread thread = new Thread(target);
        thread.setName("Zhang San");
        thread.start();

        while(target.getStop()==false){}

        System.out.println("Main Thread");
        target.restart();
    }

}
